#include "./../../lvgl/lvgl.h"
#include "./../../lvgl/demos/lv_demos.h"
#include "./../../lv_drivers/display/fbdev.h"
#include "./../../lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/album.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/setting.h"
#include "./../../inc/login.h"

int setting_mode            = SETTING_MODE_ACCOUNT;
lv_obj_t * setting_screen   = NULL;
lv_obj_t * setting_card     = NULL;
lv_obj_t * user_mode_card   = NULL;
lv_obj_t * dilink_mode_card = NULL;
lv_obj_t * volume_mode_card = NULL;

static lv_obj_t * label1 = NULL;
static lv_obj_t * label2 = NULL;
static lv_obj_t * label3 = NULL;
static lv_obj_t * label4 = NULL;

static void hide_all_modules();
static void account_event_cb(lv_event_t * e);
static void dilink_event_cb(lv_event_t * e);
static void volume_event_cb(lv_event_t * e);
static void show_mode();

void setting_init(void)
{
    setting_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(setting_screen, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(setting_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_pad_all(setting_screen, 0, LV_PART_MAIN);

    navigate_to(setting_screen);

    show_status_bar(setting_screen);

    lv_obj_t * setting_all_card = lv_obj_create(setting_screen);
    lv_obj_set_size(setting_all_card, 800, 380);
    lv_obj_set_pos(setting_all_card, 0, 40);
    lv_obj_set_style_bg_opa(setting_all_card, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_pad_all(setting_all_card, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(setting_all_card, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(setting_all_card, 0, LV_PART_MAIN);

    lv_obj_clear_flag(setting_all_card, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_t * img = lv_img_create(setting_all_card);
    lv_img_set_src(img, "S:res/img/car_bg1.png");
    lv_obj_set_size(img, 800, 480);

    show_menuList(setting_all_card);
    show_setting_card(setting_all_card);
    show_taskbar(setting_screen);
}

void show_menuList(lv_obj_t * parent)
{
    lv_obj_t * menu_list = lv_list_create(parent);

    lv_obj_set_size(menu_list, 100, 390);
    lv_obj_set_pos(menu_list, 0, 0);

    // 设置列表样式
    lv_obj_set_style_bg_color(menu_list, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(menu_list, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(menu_list, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(menu_list, 0, LV_PART_MAIN);

    lv_obj_set_style_border_width(menu_list, 1, LV_PART_MAIN);
    lv_obj_set_style_border_color(menu_list, lv_color_hex(BLACK), LV_PART_MAIN);

    lv_obj_set_style_bg_opa(menu_list, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(menu_list, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(menu_list, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    // 添加列表项
    //-------------- 账号 ----------------
    lv_obj_t * btn1 = lv_obj_create(menu_list);
    lv_obj_set_size(btn1, lv_pct(100), lv_pct(15));

    lv_obj_clear_flag(btn1, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(btn1, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(btn1, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(btn1, 0, LV_PART_MAIN);

    label1 = lv_label_create(btn1);
    lv_label_set_text(label1, "账号");
    lv_obj_set_style_text_font(label1, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(label1, lv_color_white(), LV_PART_MAIN);
    lv_obj_center(label1);

    lv_obj_add_event_cb(btn1, account_event_cb, LV_EVENT_CLICKED, NULL);

    //-------------- DiLink ----------------
    lv_obj_t * btn2 = lv_obj_create(menu_list);
    lv_obj_set_size(btn2, lv_pct(100), lv_pct(15));
    lv_obj_set_pos(btn2, 0, lv_pct(15));

    lv_obj_clear_flag(btn2, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(btn2, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(btn2, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(btn2, 0, LV_PART_MAIN);

    label2 = lv_label_create(btn2);
    lv_label_set_text(label2, "DiLink");
    lv_obj_set_style_text_font(label2, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(label2, lv_color_white(), LV_PART_MAIN);
    lv_obj_center(label2);

    lv_obj_add_event_cb(btn2, dilink_event_cb, LV_EVENT_CLICKED, NULL);

    //-------------- 车辆设置 ----------------

    lv_obj_t * btn3 = lv_obj_create(menu_list);
    lv_obj_set_size(btn3, lv_pct(100), lv_pct(15));
    lv_obj_set_pos(btn3, 0, lv_pct(30));

    lv_obj_clear_flag(btn3, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(btn3, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(btn3, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(btn3, 0, LV_PART_MAIN);

    label3 = lv_label_create(btn3);
    lv_label_set_text(label3, "车辆设置");
    lv_obj_set_style_text_font(label3, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    lv_obj_set_style_text_color(label3, lv_color_white(), LV_PART_MAIN);
    lv_obj_center(label3);

    lv_obj_add_event_cb(btn3, volume_event_cb, LV_EVENT_CLICKED, NULL);

    // lv_obj_t * btn2   = lv_list_add_btn(menu_list, LV_SYMBOL_CALL, "DiLink");
    // lv_obj_t * label2 = lv_obj_get_child(btn2, 1);
    // lv_obj_set_style_text_font(label2, &SourceHanSansCN_Bold_20, LV_PART_MAIN);
    // lv_obj_add_event_cb(btn2, dilink_event_cb, LV_EVENT_CLICKED, NULL);

    // // 设置按钮2为透明，文字和图标为白色
    // lv_obj_set_style_bg_opa(btn2, LV_OPA_0, LV_PART_MAIN);               // 完全透明背景
    // lv_obj_set_style_text_color(label2, lv_color_white(), LV_PART_MAIN); // 文本白色
}

void show_setting_card(lv_obj_t * parent)
{
    setting_card = lv_obj_create(parent);

    lv_obj_set_size(setting_card, 700, 390);
    lv_obj_set_pos(setting_card, 100, 0);

    lv_obj_set_style_bg_color(setting_card, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(setting_card, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(setting_card, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_all(setting_card, 0, LV_PART_MAIN);

    lv_obj_set_style_bg_opa(setting_card, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(setting_card, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(setting_card, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_set_style_border_width(setting_card, 1, LV_PART_MAIN);
    lv_obj_set_style_border_color(setting_card, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_clear_flag(setting_card, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 如果需要只显示上下边框，可以使用背景色覆盖左右边框
    // lv_obj_set_style_bg_color(menu_card, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    // lv_obj_set_style_bg_opa(menu_card, LV_OPA_COVER, LV_PART_MAIN);

    show_mode(); // 显示模式设置
}

// 隐藏所有模块容器
static void hide_all_modules()
{
    if(user_mode_card != NULL) {
        lv_obj_add_flag(user_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label1, lv_color_white(), LV_PART_MAIN);
    }
    if(dilink_mode_card != NULL) {
        lv_obj_add_flag(dilink_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label2, lv_color_white(), LV_PART_MAIN);
    }
    if(volume_mode_card != NULL) {
        lv_obj_add_flag(volume_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label3, lv_color_white(), LV_PART_MAIN);
    }
}

static void account_event_cb(lv_event_t * e)
{
    setting_mode = SETTING_MODE_ACCOUNT;
    show_mode();
}

static void dilink_event_cb(lv_event_t * e)
{
    setting_mode = SETTING_MODE_DILINK;
    show_mode();
}

static void volume_event_cb(lv_event_t * e)
{
    setting_mode = SETTING_MODE_VOLUME;
    show_mode();
}

static void show_mode()
{
    hide_all_modules(); // 隐藏所有模块容器

    if(setting_mode == SETTING_MODE_ACCOUNT) {
        show_user_card(setting_card);
        lv_obj_clear_flag(user_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label1, lv_color_hex(BLUE), LV_PART_MAIN);
    } else if(setting_mode == SETTING_MODE_DILINK) {
        show_dilink_card(setting_card);
        lv_obj_clear_flag(dilink_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label2, lv_color_hex(BLUE), LV_PART_MAIN);
    } else if(setting_mode == SETTING_MODE_VOLUME) {
        show_volume_card(setting_card);
        lv_obj_clear_flag(volume_mode_card, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_text_color(label3, lv_color_hex(BLUE), LV_PART_MAIN);
        
    }
}